package com.xinfan.mhl.view;

import com.xinfan.mhl.domain.*;
import com.xinfan.mhl.service.*;
import com.xinfan.mhl.utils.Utility;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Author xin
 * @Date 2022/6/22 22:23
 * @Version 1.0
 */
@SuppressWarnings({"all"})
public class MHLView {

    private boolean loop = true;

    private EmployeeService employeeService = new EmployeeService();

    private DiningTableService diningTableService = new DiningTableService();

    //初始化菜单service
    private MenuService menuService = new MenuService();

    //初始化账单service
    private BillService billService = new BillService();

    //中文账单service
    private MultiBeanService multiBeanService = new MultiBeanService();

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    public void showTableList() {
        List<DiningTable> diningTables = diningTableService.getDiningTables();
        System.out.println("餐桌\t\t状态");
        for (DiningTable diningTable : diningTables) {
            System.out.println(diningTable);
        }
        System.out.println("=============显示完毕===============");
    }

    public void orderTable() {
        System.out.println("==============预定餐桌=================");
        System.out.print("请选择要预定餐桌编号（-1退出）； ");
        int id = Utility.readInt();
        if (id == -1) {
            System.out.println("退出预定");
            return;
        } else {
            //判断桌子是否在使用中
            DiningTable diningTable = diningTableService.isUsed(id);
            if (diningTable == null) {
                System.out.println("输入的桌子不存在");
                return;
            } else if (!("空".equals(diningTable.getState()))) {
                System.out.println("该卓已被预定");
                return;
            } else {
                System.out.print("确认是否预定（Y/N）： ");
                String table_confirm = Utility.readString(1);
//                char c = Utility.readConfirmSelection();
                if ("Y".equalsIgnoreCase(table_confirm)) {
                    System.out.print("预定人名字： ");
                    String orderName = Utility.readString(10);
                    System.out.print("预定人电话： ");
                    String orderTel = Utility.readString(10);
                    boolean result = diningTableService.orderTable(orderName, orderTel, id);
                    System.out.println(result?"预定成功":"预定失败");
                } else {
                    System.out.println("退出预定");
                    return;
                }
            }
        }


    }

    //菜品显示
    public void showMenu() {
        System.out.println("==========满汉楼菜单==========");
        //获取菜单信息并展示
        List<Menu> menus = menuService.getMenus();
        System.out.println("编号\t\t菜名\t\t\t种类\t\t价格");
        for (Menu menu : menus) {
            System.out.println(menu);
        }
        System.out.println("==========菜单显示结束==========");
    }

    //点餐生成账单
    public void order() {
        System.out.println("==============开始点餐=================");
        System.out.print("请选择要点餐的餐桌编号（-1退出）； ");
        int tableId = Utility.readInt();
        if (tableId == -1) {
            System.out.println("退出点餐系统，点餐失败");
            return;
        }
        if (diningTableService.isUsed(tableId) == null) {
            System.out.println("输入的餐桌不存在 请重新输入");
            return;
        }
        System.out.print("请选择菜单编号（-1退出）； ");
        int menuId = Utility.readInt();
        if (menuId == -1) {
            System.out.println("退出点餐系统，点餐失败");
            return;
        }
        System.out.print("请选择菜品数量（-1退出）； ");

        int nums = Utility.readInt();
        if (nums == -1) {
            System.out.println("退出点餐系统，点餐失败");
            return;
        }
        boolean bill = billService.createBill(tableId, menuId, nums);
        System.out.println(bill?"点餐成功，生成订单":"点餐失败");


    }

    //账单显示
    public void showBill() {
        System.out.println("==============显示账单=================");
        List<MultiBean> multiBeans = multiBeanService.getMultiBeans();
        //List<Bill> bills = billService.getBills();
        System.out.println("菜名\t\t数量\t\t金额\t\t桌号\t\t日期\t\t状态");
        for (MultiBean multiBean : multiBeans) {
            System.out.println(multiBean);
        }



    }

    //结账系统
    public void pay() {
        System.out.println("==============结账=================");
        System.out.print("请选择要结账的餐桌编号（-1退出）； ");
        int tableId = Utility.readInt();
        if (tableId == -1) {
            System.out.println("退出结账，结账失败");
            return;
        }
        if (diningTableService.isUsed(tableId) == null) {
            System.out.println("该桌没有需要结账的账单");
            return;
        }
        char c = Utility.readConfirmSelection();
        if (c == 'N') {
            System.out.println("退出结账，结账失败");
            return;
        }
        System.out.print("输入结账方式: ");
        String payMode = Utility.readString(10);
        boolean b = billService.payBill(tableId, payMode);
        System.out.println(b?"结账成功":"结账失败");
    }

    public void mainMenu() {
        while (loop) {

            System.out.println("==========满汉楼==========");
            System.out.println(LocalDateTime.now());
            System.out.println(new Date());
            System.out.println("\t\t1 登录满汉楼");
            System.out.println("\t\t2 退出满汉楼");
            System.out.print("请输入你的选择：");
            String s = Utility.readString(1);
            if ("1".equals(s)) {
                System.out.print("请输入你的用户名:");
                String empId = Utility.readString(10);
                System.out.print("请输入你的密码:");
                String pwd = Utility.readString(10);
                //与后台交互 判断用户名和密码是否正确 service.getEmployeeByEmpIdAndPwd
                Employee employee = employeeService.getEmployeeByEmpIdAndPwd(empId, pwd);
                if (employee != null) {
                    System.out.println("==========登录成功["+employee.getName()+"]==========");
                    while (loop) {
                        System.out.println("==========满汉楼二级菜单==========");
                        System.out.println("\t\t1 显示餐桌状态");
                        System.out.println("\t\t2 预定餐桌");
                        System.out.println("\t\t3 显示所有菜品");
                        System.out.println("\t\t4 点餐服务");
                        System.out.println("\t\t5 查看账单");
                        System.out.println("\t\t6 结账");
                        System.out.println("\t\t9 退出满汉楼");
                        System.out.print("请输入你的选择：");
                        String key = Utility.readString(1);
                        switch (key) {
                            case "1":
                                showTableList();
                                break;
                            case "2":
                                orderTable();
                                break;
                            case "3":
                                showMenu();
                                break;
                            case "4":
                                order();
                                break;
                            case "5":
                                showBill();
                                break;
                            case "6":
                                pay();
                                break;
                            case "9":
                                loop = false;
                                break;
                            default:
                                System.out.println("请重新输入");
                                break;

                        }
                    }


                } else {
                    System.out.println("登录失败请重新输入");
                }

            } else if ("2".equals(s)) {

                loop = false;
                break;
            } else {
                System.out.println("你输入的内容有问题，请重新输入！！！");
            }

        }
        System.out.println("退出成功，欢迎下次光临");
    }
}
